package com.springmvc.controller;

import com.springmvc.entity.Admin;
import com.springmvc.service.AdminService;
import com.springmvc.way.Encryption;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

@Controller
public class adminController {

    @Autowired
    private AdminService adminService;

    //添加管理员
    @RequestMapping(value = "/admin/add", method = RequestMethod.POST)
    @ResponseBody
    public JSONObject addAdmin(@RequestParam("admin") String admin) {
        JSONObject json = new JSONObject();
        JSONObject adminJson = JSONObject.fromObject(admin);
        Admin adminUser = new Admin();

        Iterator iterator = adminJson.keys();
        while (iterator.hasNext()) {
            String key = iterator.next().toString();
            if (key.equals("admin_user_name")) {
                adminUser.setAdminUserName(adminJson.getString(key));
            }
            if (key.equals("admin_password")) {
                adminUser.setAdminPassword(Encryption.MD5(adminJson.getString(key)));
            }
            if (key.equals("real_name")) {
                adminUser.setRealName(adminJson.getString(key));
            }
            if (key.equals("level")) {
                adminUser.setLevel(adminJson.getInt(key));
            }
            if (key.equals("content")) {
                adminUser.setContent(adminJson.getString(key));
            }
        }
        Date date = new Date();
        adminUser.setLoginTime(date);

        if (adminService.insert(adminUser) == 1) {
            json.put("result", 1); //新增成功返回1
        } else {
            json.put("result", 0); //新增失败返回0
        }
        return json;
    }



    //管理员登录
    @RequestMapping(value = "/admin/login", method = RequestMethod.POST)
    @ResponseBody
    public JSONObject adminLogin(@RequestParam("admin_login") String admin) {
        JSONObject json = new JSONObject();
        JSONObject adminJson = JSONObject.fromObject(admin);
        Admin adminUser = new Admin();
        List<Admin> list = new ArrayList<Admin>();

        Iterator iterator = adminJson.keys();
        while (iterator.hasNext()) {
            String key = iterator.next().toString();
            if (key.equals("admin_user_name")) {
                adminUser.setAdminUserName(adminJson.getString(key));
            }
            if (key.equals("admin_password")) {
                adminUser.setAdminPassword(Encryption.MD5(adminJson.getString(key)));
            }
        }

        list = adminService.selectName(adminUser);
        if (list.size() == 0) {
            json.put("result", 0);    //账户不存在
        } else {
            if (!list.get(0).getAdminPassword().equals(adminUser.getAdminPassword())) {
                json.put("result", 1);    //密码错误
            } else {
                json.put("result", 2);      //登陆成功
                json.put("adminId", list.get(0).getId());
            }
        }
        return json;
    }

    //删除管理员
    @RequestMapping(value = "/admin/delete",method = RequestMethod.POST)
    @ResponseBody
    public JSONObject adminDel(@RequestParam("adminId") String adminId){
        JSONObject json = new JSONObject();
        if (adminService.deleteByPrimaryKey(Integer.parseInt(adminId)) == 1){
            json.put("result",1);     //成功返回result=1
        } else {
            json.put("result",0);
        }
        return json;
    }

    //管理员列表
    @RequestMapping(value = "/admin/list",method = RequestMethod.GET)
    @ResponseBody
    public JSONObject adminList(){
        JSONObject json = new JSONObject();
        ArrayList<Admin> adlist = adminService.selectlist();
        json.put("data",adlist);
        return json;
    }

    //管理员详细信息
    @RequestMapping(value = "/admin/detail",method = RequestMethod.GET)
    @ResponseBody
    public JSONObject adminList(@RequestParam("adminId") String adminId){
        JSONObject json = new JSONObject();
        Admin admin = adminService.selectByPrimaryKey(Integer.parseInt(adminId));
        json.put("data",admin);
        return json;
    }

}
